package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"goods-hub/cache"
	"goods-hub/models"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	car_to := c.PostForm("car_to")
	car_code := c.PostForm("car_code")
	car_name := c.PostForm("car_name")
	var car = models.Car{
		CarTo:   car_to,
		CarCode: car_code,
		CarName: car_name,
	}
	err := models.DB.Create(&car).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	order_no := c.PostForm("order_no")
	order_code := c.PostForm("order_code")
	order_num := c.PostForm("order_num")
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	var order = models.Order{
		OrderCode: order_no,
		OrderNo:   order_code,
		OrderNum:  order_num,
		Cid:       cid,
	}
	err := models.DB.Create(&order).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CarOrder(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
	offset := (page - 1) * size
	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	var carOrder []models.CarOrder
	/*
		SELECT
		  orders.id AS OrderID,
		  cars.car_to AS CarTo,
		  cars.car_code AS CarCode,
		  cars.car_name AS CarName,
		  orders.order_code AS OrderCode,
		  orders.order_no AS OrderNo,
		  orders.order_num AS OrderNum
		FROM
		  orders
		  LEFT JOIN cars ON orders.cid = cars.id
	*/
	redis_key := "CarOrder_id_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id AS OrderID",
				"cars.car_to AS CarTo",
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"orders.order_code AS OrderCode",
				"orders.order_no AS OrderNo",
				"orders.order_num AS OrderNum",
			).
			Joins("LEFT JOIN cars ON orders.cid = cars.id").
			Offset(offset).
			Limit(size).
			Find(&carOrder)
		jsonData, _ := json.Marshal(&carOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"order": carOrder,
			"count": count,
		},
	})
}

func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var carOrder models.CarOrder
	/*
		SELECT
		  orders.id AS OrderID,
		  cars.car_to AS CarTo,
		  cars.car_code AS CarCode,
		  cars.car_name AS CarName,
		  orders.order_code AS OrderCode,
		  orders.order_no AS OrderNo,
		  orders.order_num AS OrderNum
		FROM
		  orders
		  LEFT JOIN cars ON orders.cid = cars.id
	*/
	redis_key := "detail_id_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id AS OrderID",
				"cars.car_to AS CarTo",
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"orders.order_code AS OrderCode",
				"orders.order_no AS OrderNo",
				"orders.order_num AS OrderNum",
			).
			Joins("LEFT JOIN cars ON orders.cid = cars.id").
			Where("orders.id=?", id).
			Find(&carOrder)
		jsonData, _ := json.Marshal(&carOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": carOrder,
	})
}
